/**
 * jims
 */
package com.jims.master.modules.finance.web;


import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.finance.entity.InpAcctDetail;
import com.jims.master.modules.finance.entity.InpAcctMoney;

import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.finance.entity.InpAcctMaster;
import com.jims.master.modules.finance.service.InpAcctMasterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 结账主记录Controller
 * @author 赵宁
 * @version 2017-03-30
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/inpAcctMaster")
public class InpAcctMasterController extends BaseController {

	@Autowired
	private InpAcctMasterService inpAcctMasterService;
	
	@ModelAttribute
	public InpAcctMaster get(@RequestParam(required=false) String id) {
		InpAcctMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inpAcctMasterService.get(id);
		}
		if (entity == null){
			entity = new InpAcctMaster();
		}
		return entity;
	}

	/**
	 * 出院结账页面
	 * @param startTime
	 * @param endTime
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(String startTime,String endTime,Model model) {
		if(startTime==null || startTime==""){
			startTime= DateUtils.getDate();
		}
		if(endTime==null || endTime==""){
			endTime=DateUtils.getDate();
		}
		model.addAttribute("startTime",startTime);
		model.addAttribute("endTime",endTime);
		return "modules/finance/inpAcctIndex";
	}

	/**
	 * 结账数据查询(总数据)
 	 * @param inpAcctMaster
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(String beginTime,String endTime,InpAcctMaster inpAcctMaster, Model model) {
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(beginTime+" 00:00:00"));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime+" 23:59:59"));
        inpAcctMaster.setOrgId(UserUtils.getOrgId());
		inpAcctMaster = inpAcctMasterService.findRcptNo(inpAcctMaster);
		model.addAttribute("inpAcctMaster", inpAcctMaster);
		return "modules/finance/inpAcctInfo";
	}

	/**
	 * 结账数据查询（下部分）
	 * @param beginTime
	 * @param endTime
	 * @param inpAcctMaster
	 * @param model
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "acctDetail")
    public String acctDetail(String beginTime,String endTime,InpAcctMaster inpAcctMaster, Model model){
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(beginTime+" 00:00:00"));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime+" 23:59:59"));
		inpAcctMaster.setOrgId(UserUtils.getOrgId());
        List<InpAcctMoney> inpAcctMoneyList= inpAcctMasterService.findRcptType(inpAcctMaster);
        List<InpAcctDetail> inpAcctDetails = inpAcctMasterService.findRcptDetail(inpAcctMaster);
		model.addAttribute("acctMoney",inpAcctMoneyList);
		model.addAttribute("acctDetail",inpAcctDetails);
		return "modules/finance/inpAcctdetail";
	}

	/**
	 * 出院结算结账确认结账
	 * @param inpAcctMaster
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(String beginTime,String endTime,InpAcctMaster inpAcctMaster) {
	    StringData data=new StringData();
		inpAcctMaster.setCreateBy(UserUtils.getUser());
		inpAcctMaster.setStartDate(DateUtils.parseDate(beginTime + " 00:00:00"));
		inpAcctMaster.setEndDate(DateUtils.parseDate(endTime + " 23:59:59"));
		inpAcctMaster.setOrgId(UserUtils.getOrgId());
		int i=inpAcctMasterService.save(inpAcctMaster);
        if(i>0){
            data.setCode("success");
            data.setData("保存结账主记录成功");
        }else{
            data.setCode("error");
            data.setData("保存结账主记录失败");
        }
        return data;
	}

	/**
	 * 出院结账查询
	 * @param model
	 * @param inpAcctMaster
	 * @param request
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "acctSearch")
	public String acctSearch(Model model,InpAcctMaster inpAcctMaster,HttpServletRequest request,HttpServletResponse response){
		if(inpAcctMaster.getStartDate()==null || "".equals(inpAcctMaster.getStartDate()) ){
			 inpAcctMaster.setStartDate(DateUtils.parseDate(DateUtils.getDate("yyyy-MM-dd HH:mm:ss")));
		}
		if(inpAcctMaster.getEndDate()==null || "".equals(inpAcctMaster.getEndDate())){
			 inpAcctMaster.setEndDate(DateUtils.parseDate(DateUtils.getDate("yyyy-MM-dd HH:mm:ss")));
		}
		Page<InpAcctMaster> page=inpAcctMasterService.findPage(new Page<InpAcctMaster>(request,response),inpAcctMaster);
		model.addAttribute("page", page);
		model.addAttribute("inpAcctMaster", inpAcctMaster);
		return "modules/finance/inpAcctSearch";
	}

	/**
	 * 住院结账打印
	 * @param inpAcctMaster
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "printInpAcct")
	@ResponseBody
	public StringData printInpAcct(InpAcctMaster inpAcctMaster){
		StringData data = new StringData();
		String printPath=inpAcctMasterService.printInpAcct(inpAcctMaster);
		data.setData(printPath);
		data.setCode("success");
		return data;
	}
}